package com.vip.workflow.controller;

import com.vip.common.response.RestResponse;
import com.vip.common.response.RestResponses;
import com.vip.workflow.constant.UrlConsts;
import com.vip.workflow.service.IModelService;
import org.activiti.engine.repository.Model;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 模板管理Controller
 *
 * @author vip
 * @date 2019/11/30 14:38
 */
@RestController
@RequestMapping(value = UrlConsts.WORK_FLOW + "/model")
public class ModelController {

    @Autowired
    private IModelService modelService;

    /**
     * 分页获取模板列表
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping(value = "/list")
    public RestResponse<List<Model>> getModelList(@RequestParam Integer pageNo, @RequestParam Integer pageSize) {
        return RestResponses.newResponseFromResult(modelService.getModelList(pageNo, pageSize));
    }

    /**
     * 新建模型，返回模型页面数据
     *
     * @param response
     */
    @GetMapping("/create")
    public void addNewModel(HttpServletResponse response) {
        modelService.addNewModel(response);
    }

    /**
     * 编辑模板
     *
     * @param response
     * @param id
     */
    @GetMapping("/editor/{id}")
    public void toEditPage(HttpServletResponse response, @PathVariable("id") String id) {
        modelService.toEditPage(id, response);
    }

    /**
     * 删除指定模板
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public RestResponse<String> removeModel(@PathVariable("id") String id) {
        return RestResponses.newResponseFromResult(modelService.removeModelById(id));
    }

    /**
     * 批量删除模板
     *
     * @param ids
     * @return
     */
    @DeleteMapping("/multi")
    public RestResponse<String> removeMultiModel(@RequestBody List<String> ids) {
        return RestResponses.newResponseFromResult(modelService.removeMultiModel(ids));
    }

    /**
     * 导出模板为XML
     *
     * @param id
     * @param response
     */
    @GetMapping("/export/{id}")
    public void exportModelToXml(@PathVariable("id") String id, HttpServletResponse response) {
        modelService.exportModelToXml(id, response);
    }

}
